home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / sharew / packer / zlib / zfopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-17  |  2.3 KB  |  115 lines

  1. #include "zdef.h"
  2.  
  3. /*------------------------------*/
  4. /*    zfopen            */
  5. /*------------------------------*/
  6. #ifndef __STDC__
  7. ZFILE *zfopen (fileptr, how)
  8. char   *fileptr;
  9. char   *how;
  10. #else
  11. ZFILE *zfopen (char *fileptr, char *how)
  12. #endif
  13. {
  14.     register ZFILE *z;
  15.  
  16.     z = (ZFILE *) malloc (sizeof (ZFILE));
  17.  
  18.     z->flags          = 0;
  19.     z->maxbits        = Z_BITS;    /* user settable max # bits/code */
  20.     z->free_ent       = 0;        /* first unused entry */
  21.     z->block_compress = BLOCK_MASK;
  22.     z->clear_flg      = 0;
  23.     z->init           = 0;
  24.  
  25.     z->zeof           = (0 != 0);
  26.     z->c1             = EOF;
  27.     z->c2             = EOF;
  28.     z->bufput         = 0;
  29.     z->bufget         = 0;
  30.     z->bufend         = Z_MAXBUF - 1;
  31.  
  32.     z->maxbits        = Z_BITS;    /* user settable max # bits/code */
  33.  
  34.     /*
  35.      *   Open input file
  36.      */
  37.     if (*how == 'r')
  38.     {
  39. #ifdef ALCYON
  40.         z->file = fopenb (fileptr, "r");
  41. #else
  42.         z->file = fopen (fileptr, "rb");
  43. #endif
  44.         if (z->file == (FILE *) NULL)
  45.         {
  46.             char    tempfname[256];
  47.  
  48.             strcpy (tempfname, fileptr);
  49.             strcat (tempfname, ZEXT);
  50. #ifdef ALCYON
  51.             z->file = fopenb (tempfname, "r");
  52. #else
  53.             z->file = fopen (tempfname, "rb");
  54. #endif
  55.         }
  56.     }
  57.     else
  58.     {
  59.         /*
  60.          *   No compressed output yet, if ever...
  61.          *   Compress the file explicitly once it has been written
  62.          */
  63. #ifdef ALCYON
  64.         z->file = fopenb (fileptr, how);
  65. #else
  66.         z->file = fopen (fileptr, how);
  67. #endif
  68.         z->flags |= NOT_COMPRESSED;
  69.     }
  70.     if (z->file == (FILE *) NULL)
  71.     {
  72.         free (z);
  73.         z = (ZFILE *) NULL;
  74.     }
  75.  
  76.     /*
  77.      *   Check the magic number
  78.      */
  79.     if ((z != (ZFILE *) NULL)
  80.     && ((fgetc (z->file) != 0x1F) || (fgetc (z->file) != 0x9D)))
  81.     {
  82.         z->flags |= NOT_COMPRESSED;
  83.         fclose (z->file);
  84. #ifdef ALCYON
  85.         z->file = fopenb (fileptr, how);
  86. #else
  87.         z->file = fopen (fileptr, how);
  88. #endif
  89.         if (z->file == (FILE *) NULL)
  90.         {
  91.             free (z);
  92.             z = (ZFILE *) NULL;
  93.         }
  94.     }
  95.     if ((z == (ZFILE *) NULL) || ((z->flags & NOT_COMPRESSED) != 0))
  96.         return ((ZFILE *) z);
  97.  
  98.     z->maxbits        = fgetc (z->file);    /* set -b from file */
  99.     z->block_compress = z->maxbits & BLOCK_MASK;
  100.     z->maxbits       &= BIT_MASK;
  101.  
  102.     if (z->maxbits > Z_BITS)
  103.     {
  104.         fprintf (stderr,
  105.             "%s: compressed with %d bits; decompress can only handle %d bits\n",
  106.             fileptr, z->maxbits, Z_BITS);
  107.         exit (0);
  108.     }
  109.  
  110.     return ((ZFILE *) z);
  111. }
  112.  
  113.  
  114.  
  115.